package com.dianxun.exam.utils;

import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.util.Log;

/**
 * xml 解析工具类
 *
 * @author 徐翔龙
 * @ClassName: XmlUtil
 * @Description: TODO
 * @date 2014-3-29 上午9:58:22
 */
public class XmlUtil {

    public static final String TAG = "XmlUtil";

    /**
     * 解析特定的xml字符串    得到一个字符串数据
     *
     * @param @param  xmlStr
     * @param @return
     * @return ArrayList<String>
     * @throws
     * @Title: xmlParse
     * @Description: TODO
     */
    public static ArrayList<String> xmlParse(String xmlStr) {
        ArrayList<String> arrayList = new ArrayList<String>();
        XmlPullParserFactory factory = null;
        XmlPullParser xpparse = null;
        try {
            factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(true);
            xpparse = factory.newPullParser();
            xpparse.setInput(new StringReader(xmlStr));
            int eventType = xpparse.getEventType();
            String temp = null;
            boolean flag = true;
            while (eventType != XmlPullParser.END_DOCUMENT) {
                switch (eventType) {
                    case XmlPullParser.START_TAG:
                        temp = xpparse.getName();
                        if (temp.equals("INVENTORY") || temp.equals("UNITINFO")) {
                            break;
                        }
                        flag = true;
                        break;
                    case XmlPullParser.TEXT:
                        if (temp.equals("INVENTORY") || temp.equals("UNITINFO")) {
                            break;
                        }
                        flag = false;
                        arrayList.add(xpparse.getText());
                        break;
                    case XmlPullParser.END_TAG:
                        if (temp.equals("INVENTORY") || temp.equals("UNITINFO")) {
                            break;
                        }
                        if (flag) {
                            arrayList.add(" ");
                        }
                        break;
                }
                eventType = xpparse.next();
            }
        } catch (IOException e) {
            Log.e(TAG, e.toString());
        } catch (XmlPullParserException e1) {
            Log.e(TAG, e1.toString());
        }
        return arrayList;
    }

    public static UpdateInfo xmlParse(InputStream is) {
        UpdateInfo info = null;
        XmlPullParserFactory factory = null;
        XmlPullParser xpparse = null;
        try {
            factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(true);
            xpparse = factory.newPullParser();
            xpparse.setInput(is, "utf-8");
            int eventType = xpparse.getEventType();
            String temp = null;
            while (eventType != XmlPullParser.END_DOCUMENT) {
                switch (eventType) {
                    case XmlPullParser.START_TAG:
                        temp = xpparse.getName();
                        if (temp.equals("info")) {
                            info = new UpdateInfo();
                        } else if ("version".equals(temp)) {
                            info.setVersion(xpparse.nextText());
                        } else if ("url".equals(temp)) {
                            info.setUrl(xpparse.nextText());
                        } else if ("description".equals(temp)) {
                            info.setDescription(xpparse.nextText());
                        } else if ("filename".equals(temp)) {
                            info.setFileName(xpparse.nextText());
                        }
                        break;
                }
                eventType = xpparse.next();
            }
        } catch (IOException e) {
            Log.e(TAG, e.toString());
        } catch (XmlPullParserException e1) {
            Log.e(TAG, e1.toString());
        }
        return info;
    }

    public static List<UpdateInfo> xmlParseList(InputStream is) {
        List<UpdateInfo> list = new ArrayList<>();
        UpdateInfo info = null;
        XmlPullParserFactory factory = null;
        XmlPullParser xpparse = null;
        try {
            factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(true);
            xpparse = factory.newPullParser();
            xpparse.setInput(is, "utf-8");
            int eventType = xpparse.getEventType();
            String temp = null;
            while (eventType != XmlPullParser.END_DOCUMENT) {
                switch (eventType) {
                    case XmlPullParser.START_TAG:
                        temp = xpparse.getName();
                        if (temp.equals("info")) {
                            info = new UpdateInfo();
                        } else if ("version".equals(temp)) {
                            info.setVersion(xpparse.nextText());
                        } else if ("url".equals(temp)) {
                            info.setUrl(xpparse.nextText());
                        } else if ("description".equals(temp)) {
                            info.setDescription(xpparse.nextText());
                        } else if ("filename".equals(temp)) {
                            info.setFileName(xpparse.nextText());
                        } else if ("enable".equals(temp)) {
                            info.setEnable(xpparse.nextText());
                        }
                        break;
                    case XmlPullParser.END_TAG:
                        temp = xpparse.getName();
                        if (temp.equals("info")) {
                            list.add(info);
                        }
                        break;
                }
                eventType = xpparse.next();
            }
        } catch (IOException e) {
            Log.e(TAG, e.toString());
        } catch (XmlPullParserException e1) {
            Log.e(TAG, e1.toString());
        }
        return list;
    }
}
